package com.ruoyi.jihuo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.jihuo.domain.JhTmProduct;
import com.ruoyi.jihuo.mapper.JhTmProductMapper;
import com.ruoyi.jihuo.service.IJhTmProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@Service
public class JhTmProductServiceImpl extends ServiceImpl<JhTmProductMapper, JhTmProduct> implements IJhTmProductService {
    @Autowired
    private JhTmProductMapper jhTmProductMapper;

    /**
     * 查询商品
     *
     * @param jhId 商品主键
     * @return 商品
     */
    @Override
    public JhTmProduct selectJhTmProductByJhId(String jhId) {
        return jhTmProductMapper.selectJhTmProductByJhId(jhId);
    }

    /**
     * 查询商品列表
     *
     * @param jhTmProduct 商品
     * @return 商品
     */
    @Override
    public List<JhTmProduct> selectJhTmProductList(JhTmProduct jhTmProduct) {
        jhTmProduct.setJhUserId(SecurityUtils.getUserId());
        return jhTmProductMapper.selectJhTmProductList(jhTmProduct);
    }

    /**
     * 新增商品
     *
     * @param jhTmProduct 商品
     * @return 结果
     */
    @Override
    public int insertJhTmProduct(JhTmProduct jhTmProduct) {
        return jhTmProductMapper.insert(jhTmProduct);
    }

    /**
     * 修改商品
     *
     * @param jhTmProduct 商品
     * @return 结果
     */
    @Override
    public int updateJhTmProduct(JhTmProduct jhTmProduct) {
        return jhTmProductMapper.updateJhTmProduct(jhTmProduct);
    }

    /**
     * 批量删除商品
     *
     * @param jhIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteJhTmProductByJhIds(String[] jhIds) {
        return jhTmProductMapper.deleteJhTmProductByJhIds(jhIds);
    }

    /**
     * 删除商品信息
     *
     * @param jhId 商品主键
     * @return 结果
     */
    @Override
    public int deleteJhTmProductByJhId(String jhId) {
        return jhTmProductMapper.deleteJhTmProductByJhId(jhId);
    }

    @Override
    public List<String> getTmProductByGeMonths(int months) {
        return this.lambdaQuery()
                .select(JhTmProduct::getJhUrl)
                .ge(JhTmProduct::getJhCreatedTime, LocalDateTime.now().minusMonths(months))
                .list()
                .stream()
                .map(JhTmProduct::getJhUrl)
                .collect(Collectors.toList());
    }

    @Override
    public List<JhTmProduct> likeJhUrlOrderByDesc(String str) {
        return jhTmProductMapper.likeJhUrlOrderByDesc(str);
    }
}
